package Core;

import Controls.*;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class Form extends GUIObject
{
	protected LayoutManager form;
	public int style;
	public LayoutManager head, body, panel;

	protected Text windowCaption;
	protected Button pb1, pb2, pb3, pb4, pb5;

	public static int	HEAD=1,
						THIN_PANEL=2,
						THICK_PANEL=4,
						NO_FS=8,
						NO_BG=16,
						NO_CENTRALISE=32,
						CENTRALISE_FROM_BG=64;

	public static int StandartHeadThickness = Font.getDefaultFont().getHeight() + 7;
	public static int StandartThinPanelThickness = Font.getDefaultFont().getHeight() + 5;
	public static int StandartThickPanelThickness = 2*StandartThinPanelThickness;
	public static int StandartFSBorder = 5;
	public static int StandartBGColor = 0x6e6e6e;

	public static String StandartBGPath = "/black.cfg";

	public boolean isFullscreen = true;
	public boolean allowActionText = true;

	public Form(int style)
	{
		init(style, GUICanvas.manager.getW(), GUICanvas.manager.getH());
	}

	public Form(int style, int w, int h)
	{
		init(style, w, h);
	}
	
	private void init(int style, int w, int h)
	{
		this.style = style;
		
		if((style & NO_FS) == NO_FS) isFullscreen = false;

		if(!isFullscreen && !binFlag(style, Form.NO_CENTRALISE))
		{
			w -= StandartFSBorder*2;
			h -= StandartFSBorder*2;
		}

		W = w;
		H = h;

		form = new LayoutManager(w, h);
		form.setParentForm(this);
		form.allowToEnlarge = false;

		if((style & HEAD) == HEAD)
		{
			head = new LayoutManager(w, StandartHeadThickness);
			head.allowToEnlarge = false;
			head.type = LayoutManager.VERTICAL;
			head.h_border = 5;
			head.v_border = 1;
			windowCaption = new Text("Window", head.getAvailableW(), false);
			head.addControl(windowCaption);
			head.addControl(new Line(head.getW()-head.h_border*2, 0xffffff));
		}
		else head = new LayoutManager();

		if((style & THIN_PANEL) == THIN_PANEL)
		{
			panel = new LayoutManager(w, StandartThinPanelThickness);
			panel.allowToEnlarge = false;
			panel.type = LayoutManager.HORIZONTAL;
			panel.h_border = 2;
			panel.v_border = 2;
			int bw = (panel.getAvailableW() - 2*panel.spacing)/3;
			
			pb1 = new Button("", bw, panel.getAvailableH());
			pb2 = new Button("", panel.getAvailableW()-bw*2-panel.spacing*2, panel.getAvailableH());
			pb3 = new Button("", bw, panel.getAvailableH());

			pb1.addImage(ControlImage.NOTIFER_LS);
			pb2.addImage(ControlImage.NOTIFER_C);
			pb3.addImage(ControlImage.NOTIFER_RS);

			panel.addControl(pb1);
			panel.addControl(pb2);
			panel.addControl(pb3);
		}
		else if((style & THICK_PANEL) == THICK_PANEL)
		{
			panel = new LayoutManager(w, StandartThickPanelThickness);
			panel.allowToEnlarge = false;
			panel.type = LayoutManager.HORIZONTAL;
			panel.spacing = 0;

			if(isFullscreen) panel.h_border = panel.v_border = 1;
			else panel.h_border = panel.v_border = 2;

			int bw = (panel.getAvailableW() - 2*panel.spacing)/3;

			LayoutManager l_col = new LayoutManager(bw, panel.getAvailableH());
			l_col.spacing  = 0;
			int bh = (l_col.getAvailableH() - l_col.spacing)/2;
			pb1 = new Button("", bw, bh);
			pb4 = new Button("", bw, bh);
			//l_col.type = LayoutManager.
			l_col.addControl(pb1);
			l_col.addControl(pb4);

			LayoutManager r_col = new LayoutManager(bw, panel.getAvailableH());
			r_col.spacing = 0;
			pb3 = new Button("", bw, bh);
			pb5 = new Button("", bw, bh);
			r_col.addControl(pb3);
			r_col.addControl(pb5);

			pb2 = new Button("", panel.getAvailableW()-bw*2-panel.spacing*2, panel.getAvailableH());

			panel.addControl(l_col);
			panel.addControl(pb2);
			panel.addControl(r_col);

			pb1.addImage(ControlImage.NOTIFER_LS);
			pb2.addImage(ControlImage.NOTIFER_C);
			pb3.addImage(ControlImage.NOTIFER_RS);
			pb4.addImage(ControlImage.NOTIFER_S);
			pb5.addImage(ControlImage.NOTIFER_P);

		}
		else panel = new LayoutManager();

		body = new LayoutManager( w , h - head.H - panel.H);
		body.allowToEnlarge = false;

		form.type = LayoutManager.VERTICAL;
		form.spacing = 0;
		form.addControl(head);
		form.addControl(body);
		form.addControl(panel);

		panel.type = LayoutManager.HORIZONTAL;

		head.parent = panel.parent = body.parent = this;

		if((style & NO_BG) == 0) form.bg = GUICanvas.pool.CreateImage(StandartBGPath, w, h, StandartBGColor, isFullscreen);

		GUICanvas.manager.addForm(this);
	}

	public void draw(Graphics g, int x, int y)
	{
		//g.setClip(x, y, getW()+2, getH()+2);
		GUICanvas.clipMashine.clip(g, x, y, getW()+2, getH()+2);
		form.draw(g, x, y);
		GUICanvas.clipMashine.removeTopClip(g);
	}

	public String getHeadText()
	{
		if(windowCaption != null) return windowCaption.getCaption();
		else return "noname";
	}

	public void setActionText(String text)
	{
		if(allowActionText) setWindowText(GM.PANEL_CENTER, text);
	}

	public void setWindowText(int flag, String text)
	{
		if(((style & THIN_PANEL) == THIN_PANEL) || ((style & THICK_PANEL) == THICK_PANEL))
		{
			if(flag == GM.PANEL_LEFTSOFT && pb1 != null)	pb1.setCaption(text);
			if(flag == GM.PANEL_CENTER && pb2 != null)		pb2.setCaption(text);
			if(flag == GM.PANEL_RIGHTSOFT && pb3 != null)	pb3.setCaption(text);
		}
		if((style & THICK_PANEL) == THICK_PANEL)
		{
			if(flag == GM.PANEL_STAR && pb4 != null)	pb4.setCaption(text);
			if(flag == GM.PANEL_POUND && pb5 != null)	pb5.setCaption(text);
		}

		if(flag == GM.FORM_CAPTION && windowCaption != null) windowCaption.setCaption(text);
	}

	public void processKey(int key, int state) // Form --> Control
	{
		if((style & THIN_PANEL) == THIN_PANEL)
		{
			Button b = null;
			if(state == Key.PRESSED)
			{
				if(key == Key.LS_KEY) b = pb1;
				if(key == Key.RS_KEY) b = pb3;
				if(key == Key.P_KEY)  b = pb2;

				if(b != null) b.press();
			}
			else if(state == Key.RELEASED)
			{
				if(key == Key.LS_KEY)
				{
					b = pb1;
					onEvent(GM.PANEL_LEFTSOFT, b);
				}
				if(key == Key.RS_KEY)
				{
					b = pb3;
					onEvent(GM.PANEL_RIGHTSOFT, b);
				}
				if(key == Key.P_KEY)
				{
					b = pb2;
				}

				if(b != null) b.deselect();
			}
		}
		else if ((style & THICK_PANEL) == THICK_PANEL)
		{
			Button b = null;
			if(state == Key.PRESSED)
			{
				if(key == Key.LS_KEY) b = pb1;
				if(key == Key.RS_KEY) b = pb3;
				if(key == Key.P_KEY)  b = pb2;
				if(key == Key.POUND_KEY) b = pb5;
				if(key == Key.STAR_KEY) b = pb4;

				if(b != null) b.press();
			}
			else if(state == Key.RELEASED)
			{
				if(key == Key.LS_KEY)
				{
					b = pb1;
					onEvent(GM.PANEL_LEFTSOFT, b);
				}
				if(key == Key.RS_KEY)
				{
					b = pb3;
					onEvent(GM.PANEL_RIGHTSOFT, b);
				}
				if(key == Key.STAR_KEY)
				{
					b = pb4;
					onEvent(GM.PANEL_STAR, b);
				}
				if(key == Key.POUND_KEY)
				{
					b = pb5;
					onEvent(GM.PANEL_POUND, b);
				}
				if(key == Key.P_KEY)
				{
					b = pb2;
				}

				if(b != null) b.deselect();
			}
		}

		body.processKey(key, state);
	}

	public void close()
	{
		//System.out.println("Closing form "+getHeadText());
		GUICanvas.manager.closeUpperForm();
	}

	public int getW()
	{
		return form.getW();
	}

	public int getH()
	{
		return form.getH();
	}

	public void onEvent(int event, Object param)
	{
		if(event == GM.UPPER_FORM_CLOSED)
		{
			//System.out.println("Form event "+ getHeadText());
			Control c;
			c = body.getRealActiveControl();
			if(c != null) c.onEvent(event, param);
		}
	}

	public void setWindowIcon(ControlImage img)
	{
		if(img == null || (style & HEAD)!=HEAD) return;
		windowCaption.removeAllImages();
		windowCaption.addImage(img);
	}
}
